/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.test.swat

import com.ages.env.Options
import com.ages.graphics.GLDraw
import com.ages.graphics.Sprite
import com.ages.graphics.ViewportSwitcher
import com.ages.math.Vector3D
import com.ages.math.Vector
import com.sun.opengl.util.GLUT
import javax.media.opengl.GL
import com.ages.entity.Actor
import scala.collection.mutable.ArrayBuffer
import scala.util.Random

object Fly{
  val spr1 = new Sprite("/com/ages/test/res/fly1.png",1,1,1)
  val spr2 = new Sprite("/com/ages/test/res/fly2.png",1,1,1)
}

class Fly(v:Vector) extends Insect{
  hp = 10
  this.depth = DepthPlaces.MIDDLE
  pos = v
  bugType = Corpse.FLY

//  def think(){
//    val center = Vector3D(Options.width/2,Options.height/2,0)
//    val rnd = new Random(System.nanoTime)
//
//    val diff =  pos minus center
//    if(diff.length>300)
//      {
//        vel = (diff.unit dot -1)  dot 2
////        println(diff.unit,diff.unit.length)
//      }
//
//    vel = vel plus Vector3D((rnd.nextDouble-0.5),(rnd.nextDouble-0.5),0)
//    pos = pos plus vel
//
//    anim+=1
//    if(anim>1)
//      anim=0
//  }
  
  def draw(gl:GL)
  {
    val spot = Vector3D(pos.X,pos.Y,1)
    val box = () =>{
      gl.glEnable(GL.GL_BLEND);
//     Select blending function
      gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
     
      gl.glTranslated(-80,-70,0)
      
      if(anim==0)
        Fly.spr1.drawWithoutDisplaylist(gl, 0 , 0)
      else
        Fly.spr2.drawWithoutDisplaylist(gl, 0 , 0)
//      Smack.sprite.drawWithoutDisplaylist(gl, 0 , 0)
    }
    gl.glDisable(GL.GL_LIGHTING)
    ViewportSwitcher.setup2DView(gl)


//    val spot:Vector3D = Vector3D(pos.X,pos.Y,pos.Z-7)
    gl.glColor4d(0,0,0,0.3)
    GLDraw draw box at (spot plus Vector3D(15,15,0)) on gl
    gl.glColor4d(1,1,1,1)
    GLDraw draw box at spot on gl

//    gl.glPushMatrix()
//    gl.glTranslated(pos.X,pos.Y,1)
//
//    gl.glBegin(GL.GL_QUADS)
//    gl.glVertex2d(-20,-20)
//    gl.glVertex2d(20,-20)
//    gl.glVertex2d(20,20)
//    gl.glVertex2d(-20,20)
//    gl.glEnd()
//
//    gl.glPopMatrix()

  }

//  override def collidesWith(other:Actor):Boolean=
//  {
//    var res = false
//    if(other.isInstanceOf[Smack])
//    {
//      val diff = other.getPos minus pos
//      if(diff.length<80)
//      {
//        res = true
//      }
//    }
//    res
//  }
//
//  override def handleCollision(other:Actor){
//    if(other.isInstanceOf[Smack]){
//      val smack = other.asInstanceOf[Smack]
//
//      hp-=smack.getDmg
//      println("Fly hurt for "+smack.getDmg+", "+hp+" left")
//      if(hp<=0)
//      {
//        markForDeletion = true
//        for(s<-spawnListeners)
//        {
//          val rnd = new Random()
//          s.newWorldObject(new Corpse(pos,Corpse.FLY))
//          s.newWorldObject(new Fly(new Vector3D(800*rnd.nextDouble,600*rnd.nextDouble,0)))
//          s.increaseScore
//        }
//      }
//
//      for(s<-spawnListeners)
//      {
//        s.newWorldObject(new SmackFX(pos))
//      }
//    }
//  }


}
